package com.gthncz.hello.beans;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * 具有角色的用户数据
 * @author GT
 *
 */
public class RoledUser extends User implements UserDetails {

	private static final long serialVersionUID = 1L;
	/**
	 * 用户角色列表
	 */
	protected List<Role> roles;

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// Returns the authorities granted to the user. Cannot return null.
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		for(Role role: roles) {
			authorities.add(new SimpleGrantedAuthority(role.getName()));
		}
		return authorities;
	}

	@Override
	public String getUsername() {
		// Returns the username used to authenticate the user. Cannot return null.
		return super.getName();
	}
	
	@Override
	public String getPassword() {
		// Returns the password used to authenticate the user.
		return super.getPassword();
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Returns the password used to authenticate the user.
		return false;
	}

	@Override
	public boolean isAccountNonLocked() {
		// Indicates whether the user is locked or unlocked. A locked user cannot be authenticated.
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// Indicates whether the user's credentials (password) has expired. Expired credentials prevent authentication.
		return true;
	}

	@Override
	public boolean isEnabled() {
		// Indicates whether the user is enabled or disabled. A disabled user cannot be authenticated.
		return false == this.isDel();
	}

	@Override
	public String toString() {
		return "RoledUser [roles=" + roles + ", id=" + id + ", name=" + name + ", password=" + password + ", email="
				+ email + ", phone=" + phone + ", addtime=" + addtime + ", isDel=" + isDel + "]";
	}
	
}
